package com.never.website.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.never.website.dao.AccountDao;
import com.never.website.dao.DaoHelper;
import com.never.website.model.Account;
import com.never.website.util.GenerateMD5;


@Repository
public class AccountDaoImpl extends DaoHelper<Account> implements AccountDao{
	@SuppressWarnings("rawtypes")
	class AccountMapRow implements RowMapper{
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Account account = new Account();
			account.setId(rs.getInt("id"));
			account.setUsername(rs.getString("username"));
			account.setPassword(rs.getString("password"));
			account.setRole(rs.getString("role"));
			account.setEmail(rs.getString("email"));
			account.setActive(rs.getBoolean("active"));
			account.setCreateDate(rs.getTimestamp("create_date"));
			return account;
		}
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public int registerAcount(Account account) {
		String sql = "INSERT INTO account (id, username, password, role, email, active, create_date) VALUE (null, ?, ?, ?, ?, ?, ?)";
		Object[] parameters = new Object[]{account.getUsername(), GenerateMD5.encryptPasswordUsingMD5(account.getPassword()), account.getRole(), account.getEmail(), account.isActive(), account.getCreateDate()};
		int newId = insertOrUpdate(sql, parameters);
		return newId;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public boolean checkExistAccount(String username) {
		String sql = "SELECT COUNT(*) count_account FROM account WHERE username = ?";
		Object[] parameters = new Object[]{username};
		int countAccount = getJdbcTemplate().queryForInt(sql, parameters);
		System.out.println("countAccount : " + countAccount);
		if(countAccount != 0){
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public boolean checkExistEmail(String email) {
		String sql = "SELECT COUNT(*) count_account FROM account WHERE email = ?";
		Object[] parameters = new Object[]{email};
		int countEmail = getJdbcTemplate().queryForInt(sql, parameters);
		System.out.println("countEmail : " + countEmail);
		if(countEmail != 0){
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public List<Account> getAllAccount() {
		String sql = "SELECT * FROM account WHERE active = 1";
		List<Account> accountsList = query(sql, null, new AccountMapRow());
		return accountsList;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public Account getUserByName(String username, String role) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM account AC ");
		sql.append("WHERE AC.username = ? AND AC.role = ? AND AC.active = 1");
		Object[] parameters = new Object[]{username, role};
		List<Account> result = query(sql.toString(), parameters, new AccountMapRow());
		if(result == null){
			return null;
		}else{
			if(result.size() == 0){
				return null;
			}else{
				return result.get(0);
			}
		}
		
	}

}
